#include "contact.h"

void menu(){
    printf("------------------------------------------\n");
    printf("-----1.add                 2.del    ------\n");
    printf("-----3.search              4.modify ------\n");
    printf("-----5.show                6.sort   ------\n");
    printf("-----6.save                0.exit   ------\n");
}

static int Find_by_name(const Contact* contact,char name[MAX_NAME]){  //static表示为临时变量
    int i;
    for (i = 0; i <contact->size ; i++) {
        if(0== strcmp(name,contact->PeoInformation[i].name)){
            return i;
        }
    }
    return -1;
}

static void Add_capacity(Contact* contact){
    people* ptr=(people*) realloc(contact->PeoInformation,(contact->capacity+2)*sizeof (people));
    if(ptr==NULL){
        printf("the memory allocation is failure\n");
        return;
    }
    contact->PeoInformation=ptr;
    contact->capacity=contact->capacity+2;
    printf("allocate memory successfully\n ");
}

static void Load_contact(Contact* contact){
    people tmp={0};
    FILE* pf_read= fopen("contact.txt","rb");
    if(pf_read==NULL){
        printf("%s\n", strerror(errno));
    }
    //读取文件并将存放到通讯录里面
    while (fread(&tmp,sizeof (people),1,pf_read)){
        Add_capacity(*(&contact));
        contact->PeoInformation[contact->size]=tmp;
        contact->size++;
    }
    //fread(&tmp,sizeof (people),1,pf_read);//读一个元素到tmp中
    //返回真实读到的元素个数
    fclose(pf_read);
    pf_read=NULL;
}

void Destroy_contact(Contact* contact){
    int i;
    for(i=0;i<contact->size;i++){
        free(&(contact->PeoInformation[i]));
    }
    free(contact->PeoInformation);
    contact->PeoInformation=NULL;
}

void Add_people(Contact* contact){
    if(contact->size==contact->capacity){
        Add_capacity(contact);
    }
    printf("please input name: ");
    scanf_s("%s",contact->PeoInformation[contact->size].name);
    printf("please input age: ");
    scanf_s("%d",&(contact->PeoInformation[contact->size].age));
    printf("please input sex: ");
    scanf_s("%s",contact->PeoInformation[contact->size].sex);
    printf("please input telephone: ");
    scanf_s("%s",contact->PeoInformation[contact->size].telephone);
    printf("please input address: ");
    scanf_s("%s",contact->PeoInformation[contact->size].address);
    getchar();
    printf("add the number successfully\n");
    contact->size++;
    /*if(contact->size==MAX){
        printf("the contact is full\n");
        return;
    } else{
        printf("please input name: ");
        scanf_s("%s",contact->PeoInformation[contact->size].name);
        printf("please input age: ");
        scanf_s("%d",&(contact->PeoInformation[contact->size].age));
        printf("please input sex: ");
        scanf_s("%s",contact->PeoInformation[contact->size].sex);
        printf("please input telephone: ");
        scanf_s("%s",contact->PeoInformation[contact->size].telephone);
        printf("please input address: ");
        scanf_s("%s",contact->PeoInformation[contact->size].address);
    }*/
}

void Init_Contact(Contact* contact){
    /*memset(contact->PeoInformation,0,sizeof (contact->PeoInformation));
    contact->size=0;*/
    contact->capacity=DEFAULT_SIZE;
    contact->PeoInformation=(people*) malloc(contact->capacity*sizeof (people));
    contact->size=0;
    Load_contact(*(&contact));
}

void Show_numbers(Contact* contact){
    if(contact->size==0){
        printf("have not find numbers\n");
    } else{
        int i;
        printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n","name","age","sex","telephone","address");
        for(i=0;i<contact->size;i++){
            printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
                   contact->PeoInformation[i].name,
                   contact->PeoInformation[i].age,
                   contact->PeoInformation[i].sex,
                   contact->PeoInformation[i].telephone,
                   contact->PeoInformation[i].address);
        }
    }
}

void Del_people(Contact* contact){
    printf("Please input the people's information to be deleted\n");
    char name[MAX_NAME];
    scanf_s("%s",name);
    //查找要删除人的位置
    int position= Find_by_name(*(&contact),name);
    if(-1==position){
        printf("have not found this person's information\n");
        return;
    } else{
        int j;
        for(j=position;j<contact->size-1;j++){
            contact->PeoInformation[j]=contact->PeoInformation[j+1];
        }
        contact->size--;
        printf("delete information successfully\n");
    }
}

void Search_numbers(const Contact* contact){
    printf("Please input the people's name to be searched\n");
    char name[MAX_NAME];
    scanf_s("%s",name);
    int position= Find_by_name(*(&contact),name);
    if(position==-1){
        printf("have not found this person's information\n");
    } else{
        printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n","name","age","sex","telephone","address");
        printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
               contact->PeoInformation[position].name,
               contact->PeoInformation[position].age,
               contact->PeoInformation[position].sex,
               contact->PeoInformation[position].telephone,
               contact->PeoInformation[position].address);
    }
}

void Modify_information(Contact* contact){
    printf("Please input the people's name to be modified\n");
    char name[MAX_NAME];
    scanf_s("%s",name);
    int position= Find_by_name(*(&contact),name);
    if(position==-1){
        printf("have not found this person's information\n");
    } else{
        printf("please input name: ");
        scanf_s("%s",contact->PeoInformation[position].name);
        printf("please input age: ");
        scanf_s("%d",&(contact->PeoInformation[position].age));
        printf("please input sex: ");
        scanf_s("%s",contact->PeoInformation[position].sex);
        printf("please input telephone: ");
        scanf_s("%s",contact->PeoInformation[position].telephone);
        printf("please input address: ");
        scanf_s("%s",contact->PeoInformation[position].address);
    }
}

int compare(const void* a,const void* b){
    return (*(int*)a-*(int*)b);
}

//根据名字排序
void Sort_people(Contact* contact) {
    qsort(contact->PeoInformation, contact->size, sizeof(people), compare);
    printf("sort successfully\n");
}

void Save_contact(Contact* contact){
    FILE* pf_write= fopen("contact.txt","wb");
    if(pf_write==NULL){
        printf("%s\n", strerror(errno));
        return;
    }
    //写通讯录到文件中
    int i;
    for(i=0;i<contact->size;i++){
        fwrite(&(contact->PeoInformation[i]),sizeof (people),1,pf_write);
    }
    fclose(pf_write);
    pf_write=NULL;
    printf("save contact successfully\n");
}

